package com.ruoyi.financial.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alipay.api.AlipayApiException;
import com.ruoyi.common.core.domain.R;

import com.ruoyi.financial.domain.TCwPaymentPlan;
import com.ruoyi.financial.fegin.AlipayFegin;
import com.ruoyi.financial.service.ITCwPaymentPlanService;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2021-06-21
 */
@RestController
@RequestMapping("/plans")
@Slf4j
public class TCwPaymentPlanController extends BaseController
{


    @Autowired
    private ITCwPaymentPlanService tCwPaymentPlanService;


    @Autowired
    AlipayFegin alipayFegin;

   @Autowired
    RemoteFileService remoteFileService;


    @Autowired
    private RabbitTemplate rabbitTemplate;




    @RequestMapping("upload")
    public R<SysFile> upload(MultipartFile file) throws Exception {
        System.out.println("aaaaaaa");
        R<SysFile> data = remoteFileService.upload(file);

        System.out.println(data);
        return data;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "financial:plans:list")
    @GetMapping("/list")
    public TableDataInfo list(TCwPaymentPlan tCwPaymentPlan)
    {
        startPage();
        List<TCwPaymentPlan> list = tCwPaymentPlanService.selectTCwPaymentPlanList(tCwPaymentPlan);
        return getDataTable(list);
    }



    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "system:plan:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCwPaymentPlan tCwPaymentPlan) throws IOException
    {
        List<TCwPaymentPlan> list = tCwPaymentPlanService.selectTCwPaymentPlanList(tCwPaymentPlan);
        ExcelUtil<TCwPaymentPlan> util = new ExcelUtil<TCwPaymentPlan>(TCwPaymentPlan.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
  /*  @PreAuthorize(hasPermi = "financial:plans:query")
    @GetMapping(value = "/{id}", produces = "text/html")
    public String getInfo(@PathVariable("id") Long id) throws AlipayApiException {
        TCwPaymentPlan plan = tCwPaymentPlanService.selectTCwPaymentPlanById(id);
        System.out.println(plan);
       // alipayFegin.payOrder(plan)
      return "asd" ;
    }*/

    @GetMapping("update")
    public void update(@RequestParam("id") Long id) throws AlipayApiException {
        TCwPaymentPlan plan = tCwPaymentPlanService.selectTCwPaymentPlanById(id);
        plan.setPayState(2);
        plan.setPayMethod(2);
        plan.setPaidTime(new Date());
        System.out.println("修改支付状态");
        tCwPaymentPlanService.updateTCwPaymentPlan(plan);
        System.out.println(plan);
    }

    @RequestMapping(value="getid",method=RequestMethod.GET)
    public TCwPaymentPlan getInfos(@RequestParam("id") Long id) throws AlipayApiException {
        TCwPaymentPlan plan = tCwPaymentPlanService.selectTCwPaymentPlanById(id);
        rabbitTemplate.convertAndSend("financialExchange","",String.valueOf(plan.getId()));
        return  plan;
    }

/*    *//**
     * 新增【请填写功能名称】
     *//*
    @PreAuthorize(hasPermi = "system:plan:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCwPaymentPlan tCwPaymentPlan)
    {
        return toAjax(tCwPaymentPlanService.insertTCwPaymentPlan(tCwPaymentPlan));
    }*/

/*    *//**
     * 修改【请填写功能名称】
     *//*
    @PreAuthorize(hasPermi = "financial:plans:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCwPaymentPlan tCwPaymentPlan)
    {
        return toAjax(tCwPaymentPlanService.updateTCwPaymentPlan(tCwPaymentPlan));
    }*/

    @PostMapping("edit")
    public AjaxResult edit2(@RequestBody TCwPaymentPlan tCwPaymentPlan)
    {
        return toAjax(tCwPaymentPlanService.updateTCwPaymentPlan(tCwPaymentPlan));
    }
    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "system:plan:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tCwPaymentPlanService.deleteTCwPaymentPlanByIds(ids));
    }

    @RequestMapping("hello")
    public String hello() {
        rabbitTemplate.convertAndSend("financialExchange","","hello word!");
        return "hello word";
    }
}
